<!-- Copyright 2008 (c) - GNU GPLv3 -->

<vexi xmlns:ui="vexi://ui" xmlns="org.vexigames.footywiz">
    <ui:box fill="green" framewidth="500" frameheight="400" layout="place">
        <ui:box align="top" text="Press escape to reset" x="1" y="1" />
        <ui:box align="top" text="Press escape to reset" textcolor="white" />
        <ball_shadow id="ball_shadow" />
        <ball id="ball" />
        <player id="player" />
        
        ////////
        // player / ball vectors
        
        var ball_movex = 0;
        var ball_movey = 0;
        
        var player_movex = 0;
        var player_movey = 0;
        
        ////////
        // basic key handling
        
        var key_x = null;
        var key_y = null;
        
        var reset = function() {
            ball_movex = 0;
            ball_movey = 0;
            $ball.x = 248;
            $ball.y = 196;
            $ball_shadow.x = 248;
            $ball_shadow.y = 199;
            $player.x = 244;
            $player.y = 100;
        }
        
        KeyPressed ++= function(v) {
            cascade = v;
            switch(v) {
            case "escape":
                reset();
                break;
            case "left":
            case "right":
                key_x = v;
                break;
            case "up":
            case "down":
                key_y = v;
                break;
            }
        }
        
        KeyReleased ++= function(v) {
            cascade = v;
            if (key_x == v) key_x = null;
            if (key_y == v) key_y = null;
        }
        
        ////////
        // helpful functions
        
        var hit_x = function(px, pw, bx, bw) { return bx>=px and pw+px>=bw+bx; }
        var hit_y = function(py, ph, by, bh) { return py+ph>=by-1 and by+bh+1>=py+ph; }
        
        var max = function(a, b) { return a>b ? a : b; }
        var min = function(a, b) { return b>a ? a : b; }
        
        var frame = 0;
        
        ////////
        // main game thread
        
        vexi.thread = function(v) {
            while (true) {
                // terminate the thread if no longer valid
                if (!visible) break;
                
                ////////
                // player handling
                
                // x movement
                if (key_x == "right") {
                    if (0>player_movex) player_movex++;
                    if (3>player_movex) player_movex++;
                } else if (key_x == "left") {
                    if (player_movex>0) player_movex--;
                    if (player_movex>-3) player_movex--;
                } else {
                    if (0>player_movex) player_movex++;
                    if (player_movex>0) player_movex--;
                }
                // y movement
                if (key_y == "down") {
                    if (0>player_movey) player_movey++;
                    if (3>player_movey) player_movey++;
                } else if (key_y == "up") {
                    if (player_movey>0) player_movey--;
                    if (player_movey>-3) player_movey--;
                } else {
                    if (0>player_movey) player_movey++;
                    if (player_movey>0) player_movey--;
                }
                
                // ensure correct animation
                if (!key_x and !key_y) {
                    if ($player.state != "still") $player.state = "still";
                } else if ($player.state != "run") $player.state = "run";
                $player.x += player_movex;
                $player.y += player_movey;
                $player.anim();
                
                // collision detection
                if (hit_x($player.x, $player.width, $ball.x, $ball.width) and hit_y($player.y, $player.height, $ball.y+2, $ball.height)) {
                    ball_movex = player_movex*2;
                    ball_movey = player_movey*2;
                }
                
                ////////
                // ball handling
                
                // slow the ball down
                if (0>ball_movex) ball_movex++;
                if (ball_movex>0) ball_movex--;
                if (0>ball_movey) ball_movey++;
                if (ball_movey>0) ball_movey--;
                // ball animation
                if (ball_movex or ball_movey) {
                    var s = max(vexi.math.abs(ball_movex), vexi.math.abs(ball_movey));
                    if (s>5) s=2;
                    if (s>4) s=4;
                    if (s>3) s=7;
                    if (s>2) s=10;
                    if (!(frame%s)) $ball.anim();
                    $ball.x += ball_movex;
                    $ball.y += ball_movey;
                    $ball_shadow.x += ball_movex;
                    $ball_shadow.y += ball_movey;
                }
                
                // finish up
                if ((++frame)>99) frame = 0;
                vexi.thread.sleep(50);
            }
        }
        
        // display the frame
        vexi.ui.frame = thisbox;
        // set starting positions
        reset();
        
    </ui:box>
</vexi>